/**
 * Copyright (c) 2014 zhenrongbao.com, Inc. All Rights Reserved
 * @author: jiangshuguang
 * @Date  : 14-12-2
 * @Time  : 下午3:14
 * @brief :
 */
(function () {
    var Dialog = require('common:widget/ui/dialog/dialog.js').Dialog,
        check = require('user:widget/ui/check/check.js').check,
        $page = $('.page-buyterm'),
        dialog = new Dialog(),
        pid = P.data('pid'),
        res_amount = P.data('amount');

    P.data('coupon_amount', 0);

    $page.find('.use_coupon').bind('click', function () {
        P.data('coupon_amount', 0);
        $('.use_coupon').attr('checked', false);
        $(this).attr('checked', 'checked');
        //获取红包金额
        P.data('coupon_amount', $(this).attr('data-amount'));
        P.data('coupon_id', $(this).attr('data-id'));
        P.data('coupon_type', $(this).attr('data-type'));

        calculateAmount($(this));
        $('.coupon_info').show();
    });

    $page.find('.money-input').bind('input keyup', function () {
        calculateAmount();
        $('.buy_confirm').addClass('active');
    });

    var calculateAmount = function () {
        var buyAmount = $page.find('.money-input .amount').val();
        var couponAmount = P.data('coupon_amount');
        var buyValue = (parseFloat(buyAmount) * 1000 - parseFloat(couponAmount == undefined ? 0 : couponAmount) * 1000) / 1000;
        if (buyValue < 0 || isNaN(buyValue)) {
            buyValue = 0;
        }
        $('.buy_confirm .value').text('￥' + buyValue);
        P.data('buyValue', buyValue);
    };


    var resetCouponInfo = function () {
        $('.use_coupon').attr('checked', false);
        //获取红包金额
        delete P._data['coupon_amount'];
        delete P._data['coupon_id'];
        delete P._data['coupon_type'];
        calculateAmount();
    };


    $page.find('.btn-buy').click(function () {
        if ($(this).hasClass('btn-main')) {
            var originalAmount = $.trim($page.find('.amount').val()),
                amount = P.data('buyValue'),
                coupon_amount = P.data('coupon_amount') == undefined ? 0 : P.data('coupon_amount');

            if (!check.amount(originalAmount)) {
                return;
            }

            if (parseFloat(amount) > parseFloat(res_amount)) {
                dialog.show('您的购买金额大于您的账户余额');
                return;
            }

            if ((parseFloat(coupon_amount) == 100 && parseFloat(originalAmount)*1000 < 50000 * 1000)
                || (parseFloat(coupon_amount) == 50 && parseFloat(originalAmount)*1000 < 30000 * 1000)) {
                dialog.show('' +
                    '<div style="text-align: left">该红包不可用，您的本次购买金额需要超过' + (parseFloat(coupon_amount) == 100 ? 50000 : 30000)
                + '元。</div><div style="text-align: left">详细情况请参考<a href="/user/coupon">红包使用说明</a>。' +
                    '</div>');
                resetCouponInfo();
                return;
            }

            $(this).addClass('btn-disable').removeClass('btn-main');
            $.post('/user/purchasing', {
                amount: originalAmount,
                pid: pid,
                coupon_type: P.data('coupon_type'),
                coupon_id: P.data('coupon_id')
            }, function (ret, status) {
                location.href = '/user/buyingterm?pid=' + pid + '&trade_id=' + ret.data;
            }, "json");
        }
    });
})();